import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { SimpleTableColumn, SimpleTableComponent } from '@delon/abc';
import { NzMessageService, NzModalService, NzTabChangeEvent, UploadFile, NzNotificationService } from 'ng-zorro-antd';
import { CommService } from 'app/service/comm/comm.service';
import { Constant, ServiceResult } from '@core/constant';
import { EmitterService } from 'app/service/emit.service';
import { Router } from '@angular/router';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import { Timeout } from '@shared/descriptor/timeout';
import {srmblplanrkListService} from "../../../service/srmblplanrkList/srmblplanrkList.service";
@Component({
  selector: 'srmblplanrk3List-list',
  templateUrl: './srmblplanrk3List-list.component.html',
  styleUrls: ['./srmblplanrk3List-list.component.less']
})
export class srmblplanrk3ListComponent implements OnInit, OnDestroy {

  // region 公共字段

  visible = false;
  // pageSize
  ps = 10;
  // 搜索表单
  q: any = {
    lifnr:"",
    name1: '',
    matnr: '',
    lmatnr: '',
    maktx:'',
    lmaktx:'',
    blXuhao:'',
    blSrmx13: '库存',

    blSrmx18: ''
  };

  loading = false;
  // 表格组件
  @ViewChild('st') st: SimpleTableComponent;
  // 表格数据url
  url = this.srmblplanrkListService.list();
  // region end

  // 搜索区域显示/隐藏标记
  // 显示图片
  previewVisible = false;
  // 大图地址
  previewImage = '';
  imgSuffix = '';

  years = [];
  userGroups = [];
  brands = [];
  seasons = [];
  checkedList = [];
  data = [];
  uploadExcelUrl = this.srmblplanrkListService.uploadcarinfo();


  columns: SimpleTableColumn[] = [

    { title: '备料序号', index: 'blXuhao' },
    { title: '审批状态', index: 'blSrmx13' },
    { title: '下单状态', index: 'blSrmx18' },

    { title: '供应商编码', index: 'lifnr' },
    { title: '供应商名称', index: 'name1'},
    { title: '采购类型', index: 'blPinpai' },
    { title: '采购性质', index: 'blPinxiang' },
    { title: '物料号', index: 'blKuanhao' },

    { title: '备料物料号', index: 'blSrmx20' },
    { title: '备料物料描述', index: 'blSrmx21' },
    { title: '备料或消耗', index: 'blSrmx1' },
    { title: '工厂', index: 'blSrmx2' },
    // { title: '原材备料', index: 'blSrmx3' },
    // { title: '备半成品', index: 'blSrmx4' },
    { title: '备料数', index: 'blSrmx5' },
    { title: '实际到仓数', index: 'blSrmx6' },
    { title: '实际半成品', index: 'blSrmx7' },
    // { title: '未到仓数', index: 'blSrmx8' },
    // { title: '实际总备料数', index: 'blSrmx9' },
    // { title: '通知与实际差异', index: 'blSrmx10' },
    { title: '备注', index: 'blSrmx11' },
    { title: '附件地址', index: 'blSrmx12' },
    { title: '理论交期', index: 'blSrmx14' },
    { title: '需求日期', index: 'blSrmx15' },
    { title: '承诺交期', index: 'blSrmx16' },
    { title: '实际交期', index: 'blSrmx17' },
    { title: '采购单号', index: 'blSrmx19' }
  ];

  constructor(
    private trans: TranslateService,
    public http: _HttpClient,
    public msg: NzMessageService,
    private modalSrv: ModalHelper,
    private comm: CommService,
    private cnt: Constant,
    private emit: EmitterService,
    private router: Router,
    private modalService: NzModalService,
    private srmblplanrkListService: srmblplanrkListService,

    private notice: NzNotificationService,
  ) {}
  syncing = false;
  lastDate = null;

  ngOnDestroy() {
    if (this.inter) {
      clearInterval(this.inter);
    }
  }
  inter
  ngOnInit() {
    this.imgSuffix = this.comm.getGoodsUrl();
    this.emit.getMessage().subscribe(data => {
      if (data.text === 'RELOAD_GOODS_LIST') {
        this.q.blSrmx13 ="库存";

        this.st.load(1, this.q);
      }
    })
  }
  fullChange(val: boolean) {
    // this.scroll = val ? { y: '350px' } : { y: '230px' };
  }
  checkboxChange(list: any[]) {
    this.checkedList = list;
  }

  loaddata(wqtype){
    if(1==wqtype){
      this.q.blSrmx13 ="已下达";
      this.q.blSrmx18 ="未清";
    }
    if(2==wqtype){
      this.q.blSrmx13 ="已下达";
     }
    if(3==wqtype){
      this.q.blSrmx13 ="库存";
     }
    this.st.load(1,this.q)
  }

  downurl='';
  packatta(wqtype){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"lmatnr",
        conValue:this.q.lmatnr,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

      let dto5 ={
        conKey:"maktx",
        conValue:this.q.maktx,
      }

      conmapList.push(dto5);


      let dto6 ={
        conKey:"lmaktx",
        conValue:this.q.lmaktx,
      }

      conmapList.push(dto6);
      let dto7 ={
        conKey:"blXuhao",
        conValue:this.q.blXuhao,
      }

      conmapList.push(dto7);



      if(1==wqtype){
        let dto8 ={
          conKey:"blSrmx13",
          conValue:"已下达",
        }

        conmapList.push(dto8);
        let dto9 ={
          conKey:"blSrmx18",
          conValue:"未清",
        }

        conmapList.push(dto9);
      }
      if(2==wqtype){
        let dto8 ={
          conKey:"blSrmx13",
          conValue:"已下达",
        }

        conmapList.push(dto8);

      }
      if(3==wqtype){
        let dto8 ={
          conKey:"blSrmx13",
          conValue:"库存",
        }

        conmapList.push(dto8);
       }

    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genlfsrmrkmatnr(this.q.lifnr,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }
  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }

  close(): void {
    this.visible = false;
  }
  cancel() {
    this.repdate = null;
  }
  repdate = null;
  uploadProd(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
     console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      if(info.file.response[0].piccontent==='success'){
        this.notice.create('success', '成功', info.file.response[0].piccontent);
      }else{
        this.notice.create('error', '失败', info.file.response[0].piccontent);
      }
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }
}
